//
//  PlayFirstCell.m
//  YQYHOUSE
//
//  Created by 倪勇勤 on 16/1/2.
//  Copyright © 2016年 niyonqin. All rights reserved.
//

#import "PlayFirstCell.h"
#import "BasicImageView.h"
//设置常量
#define   count 2

@interface PlayFirstCell ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageView;
//时间片
@property (nonatomic,strong) NSTimer *timer;
@end
@implementation PlayFirstCell

-(void)awakeFromNib
{
    [self prepareScollView];
    [self preparePageView];

}
- (void)prepareScollView {

    self.scrollView.delegate = self;
    
    CGRect frame = self.scrollView.frame;
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width - 6;
    frame.size = CGSizeMake(screenW, self.scrollView.frame.size.height);
    self.scrollView.frame =frame;

    CGFloat scrollW = self.scrollView.frame.size.width;
    CGFloat scrollH = self.scrollView.frame.size.height;
    for (int i = 0; i < count; i++) {
        BasicImageView *imageView = [[BasicImageView alloc] init];
        NSString *name = [NSString stringWithFormat:@"%02d",i + 1];
        imageView.image = [UIImage imageNamed:name];
        CGFloat imageX = scrollW * (i + 1);
        imageView.frame = CGRectMake(imageX, 0, scrollW, scrollH);
        [self.scrollView addSubview:imageView];
    }
    
    BasicImageView *firstImage = [[BasicImageView alloc] init];
    NSString *imageStr = [[NSString alloc] initWithFormat:@"%02d",count];
    firstImage.image = [UIImage imageNamed:imageStr];
    firstImage.frame = CGRectMake(0, 0, scrollW, scrollH);
    [self.scrollView addSubview:firstImage];
    self.scrollView.contentOffset = CGPointMake(scrollW, 0);
    
    BasicImageView *lastImage = [[BasicImageView alloc] init];
    lastImage.image = [UIImage imageNamed:@"01"];
    lastImage.frame = CGRectMake((count + 1) * scrollW, 0, scrollW, scrollH);
    [self.scrollView addSubview:lastImage];
    self.scrollView.contentSize = CGSizeMake((count + 2) * scrollW, 0);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.pagingEnabled = YES;
    [self addTimer];
    
}

-(void)preparePageView {
   
    self.pageView.numberOfPages = count;
    self.pageView.currentPageIndicatorTintColor = [UIColor redColor];
    self.pageView.pageIndicatorTintColor = [UIColor lightGrayColor];
    self.pageView.currentPage = 0;
    //设置pagecontrol的图片
    [self.pageView setValue:[UIImage imageNamed:@"bg_page_control_seleted"] forKey:@"_currentPageImage"];
    [self.pageView setValue:[UIImage imageNamed:@"bg_page_control_un_seleted"] forKey:@"_pageImage"];

}

- (void)addTimer{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:self.timer forMode:NSRunLoopCommonModes];
}

-(void)nextImage {
    CGFloat width = self.scrollView.frame.size.width;
    NSInteger index = self.pageView.currentPage;
    
    if (index == count + 1) {
        index = 0;
    } else {
        index++;
    }
    NSLog(@"%ld",(long)index);
    [self.scrollView setContentOffset:CGPointMake((index + 1) * width, 0)animated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat width = self.scrollView.frame.size.width;
    int index = (self.scrollView.contentOffset.x + width * 0.5) / width;
    if (index == count + 2) {
        index = 1;
    } else if(index == 0) {
        index = count;
    }
    self.pageView.currentPage = index - 1;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.timer invalidate];
    self.timer = nil;
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self addTimer];
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [self scrollViewDidEndDecelerating:scrollView];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat width = self.scrollView.frame.size.width;
    int index = (self.scrollView.contentOffset.x + width * 0.5) / width;
    if (index == count + 1) {
        [self.scrollView setContentOffset:CGPointMake(width, 0) animated:NO];
    } else if (index == 0) {
        [self.scrollView setContentOffset:CGPointMake(count * width, 0) animated:NO];
    }
}

@end
